package com.qk.server.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qk.common.PageResult;
import com.qk.entity.Course;
import com.qk.mapper.CourseMapper;
import com.qk.server.CourseServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class CourseServerImpl implements CourseServer {
    @Autowired
    private CourseMapper courseMapper;

    @Override
    public PageResult<Course> findCourseByList(String name, Integer subject, Integer target, Integer page, Integer pageSize) {
        PageHelper.startPage(page,pageSize);
        List<Course> courses = courseMapper.selectCourseByList(name,subject,target);
        Page<Course> page1 = (Page<Course>) courses;
        return new PageResult<>(page1.getTotal(),page1.getResult());
    }

    @Override
    public void deleteById(Integer id) {
        courseMapper.delCourseById(id);
    }

    @Override
    public void addCourse(Course course) {
        course.setCreateTime(LocalDateTime.now());
        course.setUpdateTime(LocalDateTime.now());
        courseMapper.addCourse(course);
    }

    @Override
    public Course findCourseById(Integer id) {
        return courseMapper.selectCourseById(id);
    }

    @Override
    public void updateCourse(Course course) {
        course.setUpdateTime(LocalDateTime.now());
        courseMapper.updateCourse(course);
    }

    @Override
    public List<Course> findAll() {
        return courseMapper.findAll();
    }

    @Override
    public List<Course> findCourseBySubject(Integer subject) {
        return courseMapper.findCourseBySubject(subject);
    }
}
